<?php
/**
 * 平台月销售额 (平台基础销售额数据)
 */
namespace App\Logic\SalesAmazon;

use App\Models\Basics\SysPlat;
use App\Models\SalesAmazon\SalesAmountDay;
use App\Models\SalesAmazon\SalesAmountMonth;
use Illuminate\Support\Facades\DB;
class SalesAmountMonthly
{
    public static function init(){

        $day = date('j');
        if($day > 7){
            $dates = [
                ['start' => date('Ym01'),'end' => date('Ymd'), 'month' => date('Ym')]
            ];
        }elseif($day < 2){
            $time = strtotime("midnight first day of -1 month");
            $dates = [
                ['start' => date('Ym01',$time),'end' => date('Ymt',$time), 'month' => date('Ym',$time)]
            ];
        }else{
            $time = strtotime("midnight first day of -1 month");
            $dates = [
                ['start' => date('Ym01'),'end' => date('Ymd'), 'month' => date('Ym')],
                ['start' => date('Ym01',$time),'end' => date('Ymt',$time), 'month' => date('Ym',$time)]
            ];
        }
        foreach($dates as $date){
            $_start = $date['start'];
            $_end = $date['end'];
            $month = $date['month'];
            $amountMonth = SalesAmountDay::query()
                ->whereBetween('day',[$_start,$_end])
                ->select('plat_id','country_id','seller_sku',DB::raw('sum(amount * rate_odds) as amount'))
                ->groupBy(['plat_id','country_id','seller_sku'])
                ->get();
            if ($amountMonth) {
                foreach ($amountMonth as $item){
                    $model = SalesAmountMonth::query()
                        ->where('plat_id',$item->plat_id)
                        ->where('country_id',$item->country_id)
                        ->where('seller_sku',$item->seller_sku)
                        ->where('month',$month)
                        ->first();
                    if($model){
                        $model->amount = $item->amount;
                        $model->save();
                    }else{
                        $accountId = SalesAmountDay::query()
                            ->whereBetween('day',[$_start,$_end])
                            ->where('plat_id',$item->plat_id)
                            ->where('country_id',$item->country_id)
                            ->where('seller_sku',$item->seller_sku)
                            ->value('account_id');

                        SalesAmountMonth::query()->create([
                            'plat_id' => $item->plat_id,
                            'country_id' => $item->country_id,
                            'seller_sku' => $item->seller_sku,
                            'month' => $month,
                            'account_id' => $accountId ?: 0,
                            'amount' => $item->amount
                        ]);
                    }
                }
            }
        }
    }

    /**
     * 补录rakuten平台数据
     * @param $startDate
     * @return false
     */
    public static function initBack($startDate){

        $shopifyPlat = SysPlat::PLAT_SHOPIFY;

        $startDates = $startDate == 'null' ? strtotime("midnight first day of -1 month") : $startDate; //开始时间
        if (!strtotime($startDates)) return false;

        $time = strtotime($startDates);
        $dates = [
            [
                'start' => date('Ym01',$time),
                'end' => date('Ymt',$time),
                'month' => date('Ym',$time)
            ]
        ];

        foreach($dates as $date){
            $_start = $date['start'];
            $_end = $date['end'];
            $month = $date['month'];
            $amountMonth = SalesAmountDay::query()
                ->where('plat_id',$shopifyPlat)
                ->whereBetween('day',[$_start,$_end])
                ->select('plat_id','country_id','seller_sku',DB::raw('sum(amount * rate_odds) as amount'))
                ->groupBy(['plat_id','country_id','seller_sku'])
                ->get();
            if ($amountMonth) {
                foreach ($amountMonth as $item){
                    $model = SalesAmountMonth::query()
                        ->where('plat_id',$item->plat_id)
                        ->where('country_id',$item->country_id)
                        ->where('seller_sku',$item->seller_sku)
                        ->where('month',$month)
                        ->first();
                    if($model){
                        $model->amount = $item->amount;
                        $model->save();
                    }else{
                        $accountId = SalesAmountDay::query()
                            ->whereBetween('day',[$_start,$_end])
                            ->where('plat_id',$item->plat_id)
                            ->where('country_id',$item->country_id)
                            ->where('seller_sku',$item->seller_sku)
                            ->value('account_id');

                        SalesAmountMonth::query()->create([
                            'plat_id' => $item->plat_id,
                            'country_id' => $item->country_id,
                            'seller_sku' => $item->seller_sku,
                            'month' => $month,
                            'account_id' => $accountId ?: 0,
                            'amount' => $item->amount
                        ]);
                    }
                }
            }
        }

        /*$rakutenPlat = SysPlat::PLAT_RAKUTEN;

        $startDates = $startDate == 'null' ? strtotime("midnight first day of -1 month") : $startDate; //开始时间
        if (!strtotime($startDates)) return false;

        $time = strtotime($startDates);
        $dates = [
            ['start' => date('Ym01',$time),'end' => date('Ymt',$time), 'month' => date('Ym',$time)]
        ];

        foreach($dates as $date){
            $_start = $date['start'];
            $_end = $date['end'];
            $month = $date['month'];
            $amountMonth = SalesAmountDay::query()
                ->where('plat_id',$rakutenPlat)
                ->whereBetween('day',[$_start,$_end])
                ->select('plat_id','country_id','seller_sku',DB::raw('sum(amount * rate_odds) as amount'))
                ->groupBy(['plat_id','country_id','seller_sku'])
                ->get();
            foreach ($amountMonth as $item){
                $model = SalesAmountMonth::query()
                    ->where('plat_id',$item->plat_id)
                    ->where('country_id',$item->country_id)
                    ->where('seller_sku',$item->seller_sku)
                    ->where('month',$month)
                    ->first();
                if($model){
                    $model->amount = $item->amount;
                    $model->save();
                }else{
                    $accountId = SalesAmountDay::query()
                        ->whereBetween('day',[$_start,$_end])
                        ->where('plat_id',$item->plat_id)
                        ->where('seller_sku',$item->seller_sku)
                        ->where('country_id',$item->country_id)
                        ->value('account_id');

                    SalesAmountMonth::query()->create([
                        'plat_id' => $item->plat_id,
                        'country_id' => $item->country_id,
                        'seller_sku' => $item->seller_sku,
                        'month' => $month,
                        'account_id' => $accountId ?: 0,
                        'amount' => $item->amount
                    ]);
                }
            }
        }*/
    }
}
